<?php
/*
 * @Author: your name
 * @Date: 2020-06-05 17:32:41
 * @LastEditTime: 2020-10-27 14:18:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /special/application/api/controller/ConfigController.php
 */

namespace app\api\controller;


use app\api\model\learn_data\HostLearnDataModel;
use app\api\service\Config;
use app\common\exception\CustomException;
use app\common\file_path\NetfilterFilePath;
use app\common\model\access_log\HostAccessLogModel;
use app\common\model\FilePathModel;
use think\Db;


class ConfigController extends BaseController
{
    /**
     * @name 获取syslog配置
     * @author wx
     * @date 2020/1/8 14:26
     */
    public function syslog()
    {
        $server = new \app\api\service\Config();
        $data = $server->syslog();


        return $this->jsonOutPut($data);
    }

    public function portConfig()
    {
        $data = base64_decode(file_get_contents("php://input"));

        $server = new \app\api\service\Config();
        $data = $server->portConfig($data);

        return $this->jsonOutPut($data);
    }

    /**
     * @name 获取数据库连接配置
     * @author wx
     * @date 2020/1/14 14:34
     */
    public function database()
    {
        $config = config('database.');

        $data['ip'] = $config['hostname'];
        $data['port'] = (string) $config['hostport'];
        $data['user'] = $config['username'];
        $data['password'] = $config['password'];
        $data['name'] = $config['database'];

        return $this->jsonOutPut($data);
    }

    /**
     * @name 服务端配置获取
     * @author wx
     * @date 2020/12/2 13:55
     */
    public function server()
    {
        $server = new Config();
        $return = $server->server();

        return $this->jsonOutPut($return);
    }


    /**
     * @name 通过hostid查询数据库相关配置
     * @author wx
     * @date 2020/12/10 13:33
     */
    public function databaseByHostId()
    {
        $data = file_get_contents('php://input', 'r');
        $data = json_decode($data, true);

        if(!isset($data['hostid'])){
            throw new CustomException('hostid为必要参数', 202);
        }

        $hostId = $data['hostid'];
        if(isset($data['logId']) ){
            //修改回调
            Db::name('server_socket_message_history')->where('id', $data['logId'])->setField('callback_status', 1);
        }

        //分布式部署在服务端加几条规则,就是数据库端口可以允许中间件ip访问

        //1. 学习数据
        $hostLearnDataServer = new HostLearnDataModel();
        $hostLearnDataServer->setHostId($hostId);
        $learnData = $hostLearnDataServer->getDatabaseConfig($hostId);
        $learnDataTableName = $hostLearnDataServer->getFullTableName($hostId);
        //2. 防火墙日志
        $hostFirewallLogServer = new HostAccessLogModel();
        $hostFirewallLogServer->setHostId($hostId);
        $fireWallLog = $hostFirewallLogServer->getDatabaseConfig($hostId);
        $fireWallLogTableName = $hostFirewallLogServer->getFullTableName($hostId);

        //处理127.0.0.1
        $learnData['hostname'] = $learnData['hostname'] == '127.0.0.1' ? $_SERVER['SERVER_ADDR'] : $learnData['hostname'];
        $fireWallLog['hostname'] = $fireWallLog['hostname'] == '127.0.0.1' ? $_SERVER['SERVER_ADDR'] : $fireWallLog['hostname'];

        //返回数据
        $return['learn_data'] = [
            'database_name' => $learnData['database'],
            'table_name' => $learnDataTableName,
            'ip' => $learnData['hostname'],
            'user' => $learnData['username'],
            'password' => $learnData['password'],
            'port' => (string)$learnData['hostport'],
        ];

        $return['access_log'] = [
            'database_name' => $fireWallLog['database'],
            'table_name' => $fireWallLogTableName,
            'ip' => $fireWallLog['hostname'],
            'user' => $fireWallLog['username'],
            'password' => $fireWallLog['password'],
            'port' => (string)$fireWallLog['hostport'],
        ];

        return $this->jsonOutPut($return);
    }

}